<template>
  <!-- 加解密配置 -->
  <el-dialog
    title="加解密配置"
    :visible.sync="dialogVisible"
    width="800px"
    @close="close"
  >
    <el-form ref="form" :model="form" :rules="formRules" label-width="130px">
      <el-form-item label="系统名称:" prop="systemId">
        <el-select v-model="form.systemId" :disabled="disabled" clearable placeholder="请选择系统名称">
          <el-option
            v-for="(item, index) in systemIdList"
            :key="index"
            :label="item.systemCode + ' / ' + item.systemName"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="加解密名称:" prop="encryptDecryptName">
        <el-input
          v-model="form.encryptDecryptName"
          :disabled="disabled"
          clearable
          :maxlength="50"
          placeholder="请输入加解密名称"
        />
      </el-form-item>

      <el-form-item label="加解密类型:" prop="encryptType">
        <el-select v-model="form.encryptType" :disabled="disabled" clearable placeholder="请选择加解密类型" @change="form.method = null">
          <el-option value="1" label="对称加密" />
          <el-option value="2" label="非对称加密" />
        </el-select>
      </el-form-item>

      <el-form-item label="加解密方法:" prop="method">
        <el-select v-model="form.method" :disabled="disabled" clearable placeholder="请选择加解密方法">
          <el-option
            v-for="(item, index) in (form.encryptType ? (dictOptions[form.encryptType === '1' ? 'symmetricEncryption' : 'asymmetricEncryption']) : [])"
            :key="index"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="公钥:" prop="publicKey">
        <el-input
          v-model="form.publicKey"
          :disabled="disabled"
          type="textarea"
          :rows="3"
          clearable
          :maxlength="1000"
          placeholder="请输入公钥"
        />
      </el-form-item>

      <el-form-item v-if="form.encryptType !== '1'" label="私钥:" prop="privateKey">
        <el-input
          v-model="form.privateKey"
          :disabled="disabled"
          type="textarea"
          :rows="3"
          clearable
          :maxlength="1000"
          placeholder="请输入私钥"
        />
      </el-form-item>

      <el-form-item label="备注:" prop="remarks">
        <el-input
          v-model="form.remarks"
          :disabled="disabled"
          type="textarea"
          :rows="3"
          clearable
          :maxlength="200"
          placeholder="请输入备注"
        />
      </el-form-item>
    </el-form>

    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" :disabled="!canClick" :loading="!canClick" @click="encryptDecryptConfirm('form')">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import dictMixin from '@/utils/mixins/dict'
import { EAndDAddApi } from '@/api/serviceManage/requestPartyServiceManage'
import { getSystemListApi } from '@/api/serviceManage/requestPartyServiceManage'
export default {
  name: 'AddEncPti',
  mixins: [
    dictMixin(['asymmetricEncryption', 'symmetricEncryption'])
  ],

  data () {
    return {
      disabled: false,
      type: '1',
      canClick: true,
      dialogVisible: false,
      form: {
        systemId: null,
        encryptDecryptName: null,
        encryptType: null,
        method: null,
        publicKey: null,
        privateKey: null,
        remarks: null

      },
      formRules: {
        systemId: { required: true, message: '请选择系统名称', trigger: 'blur' },
        encryptDecryptName: { required: true, message: '请输入加解密名称', trigger: 'blur' },
        encryptType: { required: true, message: '请输入加解密方式', trigger: 'blur' },
        method: { required: true, message: '请选择加解密方法', trigger: 'blur' },
        publicKey: { required: true, message: '请输入公钥', trigger: 'blur' },
        privateKey: { required: true, message: '请输入私钥', trigger: 'blur' }
      },
      // 关联系统列表
      systemIdList: []
    }
  },

  created () {
    getSystemListApi()
      .then(({ code, data }) => {
        if (code === '00000') {
          this.systemIdList = data
        }
      })
  },
  methods: {
    close() {
      this.disabled = false
      this.dialogVisible = false
      this.$refs.form.resetFields()
    },
    // 确定按钮
    encryptDecryptConfirm (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.canClick = false
          const params = this.form
          EAndDAddApi({
            ...params
          }, this.form.id)
            .then(({ code, data }) => {
              if (code === '00000') {
                this.$message.success(`${this.form.id ? '修改成功' : '新增成功'}`)
                this.dialogVisible = false
                this.$emit('getList')
                this.form = {}
              }
            }).finally(() => {
              this.canClick = true
            })
        }
      })
    }
  }
}
</script>
